package com.dwd.servicemanager.controller;

import com.dwd.basic.utils.AjaxResult;
import com.dwd.basic.utils.PageBean;
import com.dwd.org.pojo.Department;
import com.dwd.servicemanager.mapper.ProductMapper;
import com.dwd.servicemanager.pojo.Product;
import com.dwd.servicemanager.pojo.dto.ProductDto;
import com.dwd.servicemanager.query.ProductQuery;
import com.dwd.servicemanager.service.IProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/21 15:21
 * @Version 1.0
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 新增或者修改
     *
     * @param product 如果id为空，则是新增，如果id不为空，则是修改
     * @return 返回成功还是失败
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Product product) {

        try {
            if (product.getId() == null) {
                productService.save(product);
            } else {
                productService.update(product);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
        return AjaxResult.me();
    }


    /**
     * 批量上架
     *
     * @param products
     * @return
     */
    @PostMapping("/batchUpdate")
    public AjaxResult batchRemove(@RequestBody List<Product> products) {
        try {
            //批量上架
            productService.batchRemove(products);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 批量下架
     *
     * @param products
     * @return
     */
    @PostMapping("/batchDown")
    public AjaxResult batchDown(@RequestBody List<Product> products) {
        try {
            //批量下架
            productService.batchDown(products);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            productService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
        return AjaxResult.me();
    }

    @PostMapping
    @ApiOperation("分页查询")
    public PageBean<Product> queryPage(@RequestBody ProductQuery productQuery) {
        PageBean<Product> productPageBean = productService.queryPage(productQuery);
        System.out.println(productPageBean);
        return productPageBean;
    }

    /**
     * 单个上架
     * @param id
     * @return
     */
    @PostMapping("/up/{id}")
    public AjaxResult up(@PathVariable("id") Long id) {
        try {
            productService.up(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 单个下架
     * @param id
     * @return
     */
    @PostMapping("/down/{id}")
    public AjaxResult down(@PathVariable("id") Long id) {
        try {
            productService.down(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 批量删除
     *
     * @param products
     * @return
     */
    @PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Product> products) {
        try {
            //批量删除
            productService.batchDelete(products);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }


    /**
     * 门户网站分页
     *
     * @param productQuery
     * @return
     */
    @PostMapping("/queryHomePage")
    public PageBean<Product> queryHomePage(@RequestBody ProductQuery productQuery) {
        PageBean<Product> productPageBean = productService.queryPage(productQuery);
        System.out.println(productPageBean);
        return productPageBean;
    }

    /**
     * 根据id查询产品详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Product queryDepById(@PathVariable("id") Long id) {
        return productService.queryById(id);
    }

    /**
     * 销量降序
     * @param productQuery
     * @return
     */
    @PostMapping("/queryPageSaleCount")
    public PageBean<Product> queryPageSaleCount(@RequestBody ProductQuery productQuery) {
        PageBean<Product> productPageBean = productService.queryPageSalecount(productQuery);
        return productPageBean;
    }

    /**
     * 价格降序
     * @param productQuery
     * @return
     */
    @PostMapping("/queryPageSaleprice")
    public PageBean<Product> queryPageSaleprice(@RequestBody ProductQuery productQuery) {
        PageBean<Product> productPageBean = productService.queryPageSaleprice(productQuery);
        return productPageBean;
    }

}
